/*
 * vmpref.c
 */

#include <stdio.h>
#include "vmbdoor.h"

/*
 *	Get preferrence settings
 */

int	GetPreferrence()					/* 40 bytes device info area		*/
{
	uint32 command = VMCMD_GET_SETTING;

	return VMBackDoor(NULL, NULL, &command, NULL);
}

/*
 * Set preferrence settings
 */
 
int SetPreferrence(uint32 pref)
{
	uint32 command = VMCMD_SET_SETTING;
	
	return VMBackDoor(NULL, &pref, &command, NULL);
}

static const char *prefname[] = {
	"   G\tGrab by cursor move",
	"   U\tUngrab by cursor move",
	"   S\tScroll by cursor move",
	"   C\tCopy and Paste  ",
	"   T\tTime syncronization"
};

/*
 *	print current settings
 */
void SettingList()
{
	int pref = GetPreferrence();
	
	printf("%s\t%s\n",
		prefname[0], (pref & VMPREF_GRAB_MOVE) ? "On " : "Off");

	printf("%s\t%s\n",
		prefname[1], (pref & VMPREF_UNGRAB_MOVE) ? "On " : "Off");
         
	printf("%s\t%s\n",
		prefname[2], (pref & VMPREF_SCROLL_MOVE) ? "On " : "Off");

	printf("%s\t%s\n",
		prefname[3], (pref & VMPREF_COPY_PASTE) ? "On " : "Off");
        
	printf("%s\t%s\n",
		prefname[4], (pref & VMPREF_SYNCRONIZE) ? "On " : "Off");
}

/*
 * print usage help
 */
void Usage()
{
	printf("Usage: vmpref [[<action>]<feature>]\n"
		"\n"
		"<action>\n"
		"  +\tEnables specified feature.\n"
		"  -\tDisables specified feature.\n"
		"When <action> option is omitted, the default\n"
		"action is to 'toggle' current setting.\n"
		"\n"
		"<feature>\n"
		"%s\n%s\n%s\n%s\n%s\n"
		"<feature> options are case insensitive.\n"
		"When <feature> option is omitted, preference\n"
		"setting is not changed.\n",
		prefname[0], prefname[1], prefname[2], prefname[3], prefname[4]);
}

/*
 * main
 */
int main (int argc, char **argv)
{
	int toggle = 0;
	int prefmask = 0;
	int pref;
	
	/* are we running in VMware ? */
	if (VMGetVersion() == 0) {
		printf("Not running inside VMware.\n");
		return 0;
	}

	/* any command line options ? */
	if (argc > 1) {
		argv++;
		
		if (**argv == '+') {
			(*argv)++;
			toggle = 1;
		}
		else if (**argv == '-') {
			(*argv)++;
			toggle = -1;
		}

		switch (**argv) {
		case 'G':
		case 'g':
			prefmask = VMPREF_GRAB_MOVE;
			break;
		case 'U':
		case 'u':
			prefmask = VMPREF_UNGRAB_MOVE;
			break;
		case 'S':
		case 's':
			prefmask = VMPREF_SCROLL_MOVE;
			break;
		case 'C':
		case 'c':
			prefmask = VMPREF_COPY_PASTE;
			break;
		case 'T':
		case 't':
			prefmask = VMPREF_SYNCRONIZE;
			break;
		case '\0':
			break;
		default:
			Usage();
			return 0;
		}

		if (prefmask) {
			pref = GetPreferrence();
			
			if (toggle > 0) {
				pref |= prefmask;
			}
			else if (toggle < 0) {
				pref &= ~prefmask;
			}
			else {
				if (pref & prefmask) {
					pref &= ~prefmask;
				}
				else {
					pref |= prefmask;
				}
			}
			
			SetPreferrence(pref);
		}
	}

	SettingList();

	return 1;
}
/* End of File */
